package com.xyh.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyh.commonutils.JwtUtils;
import com.xyh.commonutils.MD5;
import com.xyh.educenter.entity.UcenterMember;
import com.xyh.educenter.entity.vo.RegisterVo;
import com.xyh.educenter.mapper.UcenterMemberMapper;
import com.xyh.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyh.servicebase.exceptionHandler.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-13
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    //登陆的方法
    @Override
    public String login(UcenterMember member) {
        //获取登陆手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"登陆失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if (mobileMember == null){
            throw new GuliException(20001,"登陆失败");
        }

        //判断密码
        //因为存储到数据库的密码是经过加密的
        //把输入的密码进行加密，在和数据库密码进行比较
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new GuliException(20001,"登陆失败");
        }

        //判断用户是否别禁用
        if (mobileMember.getIsDisabled()){
            throw new GuliException(20001,"登陆失败");
        }

        //登陆成功
        //生成 token 字符串 ， 使用 jwt 工具类
        String token = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return token;
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        return ucenterMember ;
    }

    //查询某一天的注册人数
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }

    //注册的数据
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String mobile = registerVo.getMobile();  //手机号
        String code = registerVo.getCode();  //验证码
        String nickname = registerVo.getNickname();  //用户
        String password = registerVo.getPassword();  //密码

        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败");
        }

        //判断手机验证码是否正确
        //先获取存在 redis 中的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new GuliException(20001,"注册失败");
        }

        //判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0){
            throw new GuliException(20001,"注册失败");
        }

        //数据添加到数据库中
        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(registerVo,ucenterMember);
        ucenterMember.setPassword(MD5.encrypt(registerVo.getPassword()));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("https://pic3.zhimg.com/80/v2-798a1d572955f1164ee0c07e4f5fa0aa_720w.jpg");
        int insert = baseMapper.insert(ucenterMember);
        if (insert == 0){
            throw new GuliException(20001,"注册失败");
        }
    }
}
